import React, { useEffect, useState } from 'react'
import "./index.less";

export const TimeBar = () => {
    const [courentTime, setCourentTime] = useState()
    const [intervalData, setInterData] = useState('')
    const getTime = () => {
        let dt = new Date();
        var y = dt.getFullYear();
        var mt = dt.getMonth() + 1;
        var day = dt.getDate();
        var h = dt.getHours(); //获取时
        var m = dt.getMinutes(); //获取分
        var s = dt.getSeconds(); //获取秒
        const Time =
            y +
            "年" +
            mt +
            "月" +
            day +
            "-" +
            h +
            "时" +
            m +
            "分" +
            s +
            "秒";
        return Time
    }
    useEffect(() => {
        const t = setInterval(() => {
            setCourentTime(getTime())
        }, 1000)
        setInterData(t)
        return () => {
            clearInterval(intervalData)
        }
    }, [])

    return <div className='header-box'>
        <div class="showTime">当前时间：{courentTime}</div>
    </div>
}